
package me.liuhui.mall.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.liuhui.mall.common.base.vo.ResultVO;
import me.liuhui.mall.manager.cache.RedisCache;
import me.liuhui.mall.manager.service.UserService;
import me.liuhui.mall.manager.service.convertor.UserConverter;
import me.liuhui.mall.manager.service.dto.user.ChangeStatusDTO;
import me.liuhui.mall.manager.service.dto.user.ListUserDTO;
import me.liuhui.mall.manager.service.dto.user.ModifyUserDTO;
import me.liuhui.mall.manager.service.vo.user.ListUserVO;
import me.liuhui.mall.repository.mapper.SessionTokenMapper;
import me.liuhui.mall.repository.mapper.UserMapper;
import me.liuhui.mall.repository.model.SessionToken;
import me.liuhui.mall.repository.model.User;
import me.liuhui.mall.repository.model.enums.SessionTokenStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * Created on 2020/10/14 20:12
 * <p>
 * Description: [TODO]
 * <p>
 *
 * @author [清远]
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserConverter userConverter;
    @Resource
    private UserMapper userMapper;
    @Resource
    private SessionTokenMapper sessionTokenMapper;
    @Resource
    private RedisCache redisCache;

    @Override
    public ResultVO<ListUserVO> list(ListUserDTO dto) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        Page<User> page = new Page<>(dto.getPage(), dto.getLimit());
        Page<User> userPage = userMapper.selectPage(page, wrapper);
        ListUserVO vo = new ListUserVO();
        vo.setTotal(userPage.getTotal());
        vo.setList(userConverter.toVo(userPage.getRecords()));
        return ResultVO.success(vo);
    }

    @Override
    public ResultVO<Boolean> changeStatus(ChangeStatusDTO dto) {
        User user = userMapper.selectById(dto.getId());
        if (user == null) {
            return ResultVO.buildFailResult("用户不存在");
        }
        if (!Objects.equals(user.getStatus(), dto.getStatus())) {
            user.setStatus(dto.getStatus());
            userMapper.updateById(user);
        }
        LambdaQueryWrapper<SessionToken> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SessionToken::getUserId, dto.getId());
        wrapper.eq(SessionToken::getStatus, SessionTokenStatus.VALID.getCode());
        List<SessionToken> sessionTokens = sessionTokenMapper.selectList(wrapper);
        for (SessionToken st : sessionTokens) {
            st.setStatus(SessionTokenStatus.INVALID.getCode());
            sessionTokenMapper.updateById(st);
            redisCache.delete("mall:token:" + st.getSessionToken());
        }

        return ResultVO.success();
    }


    @Override
    public ResultVO<Boolean> modify(ModifyUserDTO dto) {
        User user = userMapper.selectById(dto.getId());
        if (user == null) {
            return ResultVO.buildFailResult("角色不存在");
        }


        User entity = userConverter.modifyDtoToEntity(dto);
        userMapper.updateById(entity);
        return ResultVO.success();
    }

    @Override
    public ResultVO<Boolean> delete(Set<Long> ids) {
        removeBatchByIds(ids);
        return ResultVO.success();
    }

}
